﻿from rest_framework.response import Response
from apps.HttpTestcase.models import TestcaseReports
from apps.HttpTestcase.serializers import ReportsSerializer
from apps.HttpTestcase.filters import Test_reportsFilter
from rest_framework.filters import OrderingFilter
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework.generics import DestroyAPIView
from utils.viewsets import *
from utils.response import SuccessResponse


class ReportViewSet(ModelViewSet):
    queryset = TestcaseReports.objects.filter(is_delete=False)
    serializer_class = ReportsSerializer
    # 过滤
    filter_class = Test_reportsFilter
    # filter_fields = ['id']
    # 指定权限类
    # permission_classes = [permissions.IsAuthenticated]
    perms_map = {'get': '*', 'post': 'case_reports_create',
                 'put': 'case_reports_update', 'delete': 'case_reports_delete'}
    # 排序
    filter_backends = [DjangoFilterBackend, OrderingFilter]
    filter_fields = ['execute_time', 'version']
    ordering_fields = ['execute_time', 'version']


class TestReportsDelete(DestroyAPIView):
    perms_map = {'delete': 'case_reports_delete'}

    def delete(self, request, *args, **kwargs):
        data = request.data
        testlist = data['id']
        testlist_id = []
        for item in testlist:
            testlist_id.append(item.get('id'))

        if data.get('is_de'):
            TestcaseReports.objects.filter(id__in=testlist_id).delete()
        else:
            TestcaseReports.objects.filter(id__in=testlist_id).update(is_delete=True)

        return SuccessResponse(data=None, msg="删除成功！")
